<template>
    <el-container class="home-container">
        <el-header>
            <div>
                <img src="@/assets/my-logo.png" alt="" height="40px" width="40px" class="logo">
                <span>学生管理辅助系统</span>
            </div>
             <div>
                <span>消息</span><i class="el-icon-chat-line-round top-i" @click="to_msg('msg')"><span class="badge red">{{unread_msg_total}}</span></i>
                <span>通知</span><i class="el-icon-message-solid top-i" @click="to_msg('notice')"><span class="badge green">{{unread_notice_total}}</span></i>
                <span>任务</span><i class="el-icon-message top-i" @click="to_msg('task')"><span class="badge warning">{{unread_task_total}}</span></i>
                <el-dropdown @command="handleCommand">
                    <!-- <el-button type="info" style="background-color: #303133">
                        {{ $store.getters.username }}<i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button> -->
                     <span class="el-dropdown-link">
                        <img :src="$store.getters.avatar_url" height="40" width="40" class="avatar" />
                        <i class="el-icon-arrow-down"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="personal"><i class="el-icon-user-solid"></i>个人中心</el-dropdown-item>
                        <el-dropdown-item command="reset_pwd"><i class="el-icon-key"></i>修改密码</el-dropdown-item>
                        <el-dropdown-item command="logout"><b><i class="el-icon-switch-button"></i>退 出</b></el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </el-header>
        <el-container>
            <el-aside :width="isCollapse ? '64px' : '200px'">
              <div class="toggle-button" @click="toggleCooapse"> | | |</div>
              <el-menu 
              background-color="#333744" text-color="white" 
              active-text-color="#409EFF" 
              unique-opened 
              :collapse="isCollapse"
              :collapse-transition="false"
              router
              :default-active="$route.path"
              >
                <el-submenu :index="items.ID + ''" v-for="items in menuList" :key="items.ID">
                    <template slot="title">
                        <i :class="items.icon"></i>
                        <span>{{items.name}}</span>
                    </template>
                    <el-menu-item :index="'/' + item.path" v-for="item in items.children" :key="item.ID">
                        <template slot="title">
                            <i :class="item.icon"></i>
                            {{item.name}}
                        </template>
                    </el-menu-item>
                </el-submenu> -->
                </el-menu>
            </el-aside>
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    data(){
        return {
            menuList: [],
            icons :{
                "1":'iconfont icon-user',
                "2":"iconfont icon-tijikongjian",
                "3":'iconfont icon-shangpin',
                "4":'iconfont icon-danju',
                "5":"iconfont icon-baobiao",
            },
            isCollapse: false,
            avtivePath: '',
            unread_msg_total: 0,
            unread_notice_total: 0,
            unread_task_total: 0,
        }
    },
    created(){
        this.getMenuList();
        this.getUnReadList();
    },
    computed: {
        matched() {
            return this.$route.matched
        }
    },
    methods: {
        handleCommand(command) {
            if (command === 'logout'){
                this.$message.success('退出成功');
                this.logout();
            }else if (command === "personal") {
                this.$message.success('正在跳转到个人中心');
                this.$router.push("/welcome")
            }else if (command === "reset_pwd") {
                this.$message.success('跳转到重置密码中心');
                this.$router.push("/reset_pwd")
            }
        },
        to_msg(name){
            if (name === 'msg'){
                this.$router.push("/messages")
            }else if (name === 'notice'){
                this.$router.push("/notices")
            }else if (name === 'task'){
                this.$router.push("/tasks")
            }
        },
        logout(){
            window.localStorage.clear();
            window.sessionStorage.clear();
            this.$router.push("/Login");
        },
        async getMenuList(){
            const {data: res, status: status} = await this.axios.get("menus/getRoleMenuTree")
            if (status === 200) {
                this.menuList = res.data.menus;
                this.$store.commit('add_menu_path', res.data.menu_path_map)
            }else if(status >= 250) {
                this.$router.push("/login");
            }else{
                this.$message.error(res.msg);
            }
        },
        async getUnReadList() {
            const {data: res, status: status} = await this.axios.get('message/getMessageCount');
            if (status !== 200) {
                return this.$message.error('获取未读消息失败！')
            }
            this.unread_msg_total = res.data.msg_total;
            this.unread_notice_total = res.data.notice_total;
            this.unread_task_total = res.data.task_total;
        },
        toggleCooapse(){
            this.isCollapse = !this.isCollapse;
        },
    }
}
</script>
<style lang="less" scoped>
.logo{
    border-radius: 50%;
    margin-left: 20px;
}
.home-container{
    height: 100%;
}
.el-header{
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: white;
    font-size: 20px;
     background-color: #373d41;
    > div{
        display: flex;
        align-items: center;
        span{
           margin-left: 10px;
           font-size: 18px;
           cursor: pointer;
        }
    }
}

.top-i{
    display: flex;
    align-items: center;
    width:70px;
    font-size: 16px;
    margin-left: 2px;
    cursor: pointer;
}
.el-header > div > i > span.badge{
    margin-left: 7px;
    display: inline-block;
    width:25px; 
    height:25px; 
    border-radius: 50%;
    font-size: 14px;
    text-align: center;
    line-height: 25px;
}

.red{
    background-color:red;
}

.green{
    background-color: forestgreen;
}

.warning{
    background-color:  #3b8dbd;
}

.el-aside{
    background-color: #333744;
    .el-menu{
        border-right: none;
    }
}

.is-active{
    background-color: #222222 !important;
}
.el-main{
    background-color: #eaedf1;
}

.iconfont{
    margin-right: 10px;
}

.toggle-button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: white;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
.avatar{
   margin-right: 10px;
   border-radius: 50%;
   border: 3px solid white;
   cursor:pointer
}
</style>